package com.codeying.dao;

import com.codeying.entity.*;
import com.codeying.utils.*;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;

import java.util.*;
/**
 *  租车订单Dao，和数据库互动。
 */
public class RentorderDao extends AbstractDao<Rentorder>{

    @Override
    protected Class<Rentorder> clazz() {
        return Rentorder.class;
    }

    @Override
    public String tbName(){
        return "tb_rentorder";
    }

    public static RentorderDao me = new RentorderDao();

    /**
     * 将map参数转换成sql的where条件语句。
     */
    @Override
    public String toConditionSql(Map<String, String> paramMap){
        StringBuilder sqlBuilder=new StringBuilder("");//接下来拼接查询条件,比如 and xx=xx and XX < xx
        if(paramMap!=null){
                String id = paramMap.get("id");
            if(MyUtils.isNotEmpty(id)){
                        sqlBuilder.append(" and id like '%").append(id).append("%'");// 使用模糊查询
                    }
                    String userid = paramMap.get("userid");
            if(MyUtils.isNotEmpty(userid)){
                         sqlBuilder.append(" and userid = '").append(userid).append("'");
                    }
                    String cid = paramMap.get("cid");
            if(MyUtils.isNotEmpty(cid)){
                         sqlBuilder.append(" and cid = '").append(cid).append("'");
                    }
                    if(MyUtils.isNotEmpty(paramMap.get("createtimeL"))){
                sqlBuilder.append(" and createtime >= '").append(paramMap.get("createtimeL").replace("T"," ")).append("'");
            }
            if(MyUtils.isNotEmpty(paramMap.get("createtimeR"))){
                sqlBuilder.append(" and createtime <= '").append(paramMap.get("createtimeR").replace("T"," ")).append("'");
            }
                    if(MyUtils.isNotEmpty(paramMap.get("priceL"))){
                sqlBuilder.append(" and price >= ").append(paramMap.get("priceL"));
            }
            if(MyUtils.isNotEmpty(paramMap.get("priceR"))){
                sqlBuilder.append(" and price <= ").append(paramMap.get("priceR"));
            }
                    if(MyUtils.isNotEmpty(paramMap.get("countL"))){
                sqlBuilder.append(" and count >= ").append(paramMap.get("countL"));
            }
            if(MyUtils.isNotEmpty(paramMap.get("countR"))){
                sqlBuilder.append(" and count <= ").append(paramMap.get("countR"));
            }
                    if(MyUtils.isNotEmpty(paramMap.get("tpriceL"))){
                sqlBuilder.append(" and tprice >= ").append(paramMap.get("tpriceL"));
            }
            if(MyUtils.isNotEmpty(paramMap.get("tpriceR"))){
                sqlBuilder.append(" and tprice <= ").append(paramMap.get("tpriceR"));
            }
                    String status = paramMap.get("status");
            if(MyUtils.isNotEmpty(status)){
                         sqlBuilder.append(" and status = '").append(status).append("'");
                    }
                //限制 用户 只能查询自己相关的信息
            String useridFrn=(String)paramMap.get("userid");//只能查看和自己相关的内容
            if(MyUtils.isNotEmpty(useridFrn)){
                sqlBuilder.append(" and userid = '").append(useridFrn).append("'");
            }
            //排序
            String orderby = paramMap.get("orderby");
            if(orderby!=null){
                sqlBuilder.append(" order by ");
                String[] orderbys = orderby.split(",");
                for (int i = 0 ; i < orderbys.length ; i++) {
                    String s = orderbys[i];
                    sqlBuilder.append(s);
                    if(i!=orderbys.length-1){
                        sqlBuilder.append(",");
                    }
                }
            }
        }
        return sqlBuilder.toString();
    }

    /**
    * 插入
    */
    @Override
    public int insert(Rentorder entity){
        try{
            //构造sql语句
            List<Object> paramList = new ArrayList<>();
            StringBuilder reParamNames = new StringBuilder();
            StringBuilder reParams = new StringBuilder();
            paramList.add(entity.getId());
                if(entity.getUserid() != 0){
                reParamNames.append(",userid");
                reParams.append(",?");
                paramList.add(entity.getUserid());
            }
                if(entity.getCid() != 0){
                reParamNames.append(",cid");
                reParams.append(",?");
                paramList.add(entity.getCid());
            }
                if(entity.getCreatetime() != null){
                reParamNames.append(",createtime");
                reParams.append(",?");
                paramList.add(entity.getCreatetime());
            }
                if(entity.getPrice() != null){
                reParamNames.append(",price");
                reParams.append(",?");
                paramList.add(entity.getPrice());
            }
                if(entity.getCount() != null){
                reParamNames.append(",count");
                reParams.append(",?");
                paramList.add(entity.getCount());
            }
                if(entity.getTprice() != null){
                reParamNames.append(",tprice");
                reParams.append(",?");
                paramList.add(entity.getTprice());
            }
                if(entity.getStatus() != null){
                reParamNames.append(",status");
                reParams.append(",?");
                paramList.add(entity.getStatus());
            }
                QueryRunner qr = new QueryRunner();  //创建QueryRunner类对象
            //不为空的插入
            String sqlBuilder = "INSERT INTO tb_rentorder (id"+reParamNames.toString()+")VALUES(?"+reParams.toString()+")";
            log(sqlBuilder.toString());
            return qr.update(JdbcUtils.getConnection(), sqlBuilder, paramList.toArray());  // 执行SQL语句
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("RentorderDao -> insert(-)报错了");
        }
        return 0;
    }

    /**
    * 根据Id更新
    */
    @Override
    public int updateById(Rentorder entity) {
        try{
            QueryRunner qr = new QueryRunner();  //创建QueryRunner类对象
            StringBuilder sqlBuilder = new StringBuilder("update tb_rentorder set id = ? ");
            List<Object> paramList = new ArrayList<>();
            paramList.add(entity.getId());
                    if(entity.getUserid() != 0){
                sqlBuilder.append(", userid = ? ");
                paramList.add(entity.getUserid());
            }
                    if(entity.getCid() != 0){
                sqlBuilder.append(", cid = ? ");
                paramList.add(entity.getCid());
            }
                    if(entity.getCreatetime() != null){
                sqlBuilder.append(", createtime = ? ");
                paramList.add(entity.getCreatetime());
            }
                    if(entity.getPrice() != null){
                sqlBuilder.append(", price = ? ");
                paramList.add(entity.getPrice());
            }
                    if(entity.getCount() != null){
                sqlBuilder.append(", count = ? ");
                paramList.add(entity.getCount());
            }
                    if(entity.getTprice() != null){
                sqlBuilder.append(", tprice = ? ");
                paramList.add(entity.getTprice());
            }
                    if(entity.getStatus() != null){
                sqlBuilder.append(", status = ? ");
                paramList.add(entity.getStatus());
            }
                //最后拼接id条件
            sqlBuilder.append("where id = ?");
            paramList.add(entity.getId());
            log(sqlBuilder.toString(),paramList);
            return qr.update(JdbcUtils.getConnection(), sqlBuilder.toString(), paramList.toArray());  // 执行SQL语句
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("RentorderDao -> updateById(-)报错了");
        }
        return 0;
    }

}

